

bcd16 = NumberPreserve
bcd16string = ASCIINumber

Rotate_Cursor_Mode:		;If the cursor is on,
				;turn it off.  If it is off,
				;turn it on.


 ld iy, flags
 SET Wait_Between_Presses, (IY + flag)	;Prevents the user
						;from accidentally
						;scrolling the cursor
						;simply by holding the respective key down

 


 BIT Ship_Mode, (IY + flag + 2)
 jr z, _
 RES Ship_Mode, (IY + flag + 2)
 ld de, (DeltaPreserve)
 ld (delta), de

_
 BIT Cursor_Mode, (IY + flag)
 JR z, _
 RES Cursor_Mode, (IY + flag)
 RES Object_Ready_To_Add, (IY + flag)
 ld de, (DeltaPreserve)
 ld (delta), de

 RET

_
 RES Menu_Mode, (IY + flag)
 SET Cursor_Mode, (IY + flag)	;Turns on cursor mode.	
 ld a, 48				;The cursor's starting position
 ld (Select_Cursor_X),a
 ld a, 16
 ld (Select_Cursor_Y),a
 ld de, (delta)
 ld (DeltaPreserve), de

 ld de, 16
 ld (delta), de

Adjust_Map_X_Y_For_Cursor:		;We want the cursor to select tiles, not partial
					;tiles.  By adjusting the MapX and MapY, we make sure
					;that the cursor lands completely on a tile.

 ld hl, (MapX)

 BIT 3,l				
 jr z, _
 ld a, l
 and %11110000
 
 add a, 16
 ld l, a

 ld (MapX), hl 

 jr ++_

_

 ld a,l
 and %11110000
 ld l, a
 ld (MapX), hl 

_


 ld hl, (MapY)
 BIT 3,l
 jr z, _
 ld a, l
 and %11110000

 add a, 16
 ld l, a

 ld (MapY), hl 

 RET
_
 ld a, l
 and %11110000
 ld l, a
 ld (MapY), hl 


 RET


Move_Select_Cursor_Down:

 CALL movemapdown
 ret

Move_Select_Cursor_Up:


 CALL movemapup
 ret

Move_Select_Cursor_Left:

 ld a, (Select_Cursor_X)

 cp 32
 jr z, _
 sub 16
 ld (Select_Cursor_X),a

 ret

_

 CALL movemapleft
 ret


Move_Select_Cursor_Right:

 ld a, (Select_Cursor_X)

 cp 48
 jr z, _
 add a, 16
 ld (Select_Cursor_X),a

 ret


_


 CALL movemapright
 ret


Draw_Select_Cursor:		;Draws the cursor.  Sometimes the cursor is a cross, and
				;sometimes it's a building to be drawn.

 BIT Object_Ready_To_Add, (IY + flag)	;If the player wants to construct a building,
						;the cursor is not a cross, but instead the building
						;to construct.
 jp nz, ++_

 di

 ld b, 16
 ld c, 2
 ld a, (Select_Cursor_X)
 ld d, a
 ld a, (Select_Cursor_Y)
 ld e, a
 ld ix, Select_Cursor
 ld iy, Select_Cursor_Mask
 call LargeClippedSprite
 ld iy, flags
 ei



 ;call Find_If_Tile_Occupied_By_Building

 ;or a
 ;jr z, _
 
 ;call Draw_Building_Info


 ret

_




_

	;When the player is placing a building, the cursor uses a sprite formed from the tiles.
	;If the sprite has not been loaded into RAM, that needs to happen.  BUT, it only
	;needs to happen once.

 BIT Cursor_Bitmap_Done, (IY + Flag)
 jp nz, Building_Cursor_Saved		;No need to load the sprite again.

					;Doing so will slow the program down.

 ld hl, Operation_Center
 ld d,0
 ld a, (BuildingSelected)
 

 rlca \ rlca \ rlca \ rlca	;Divide de by 32 to get to the proper data.


 ld e,a
  
 add hl, de				;HL points to the start tile for the building.
 add hl, de

 ld c, (hl)	;Start tile.  We need this for saving a sprite for drawing.
 ld b, 0

 rl c \ rl b
 rl c \ rl b
 rl c \ rl b
 rl c \ rl b


 inc hl

 ld a, (hl)	;Register A contains the type of building.

 ;See if the building will cost too much.

 inc hl
 inc hl

 ld e, (hl)
 inc hl
 ld d, (hl)	;DE now contains the cost of the building.
		;We see if the player has enough
		;money for the building.

 ld hl, (Player1 + Player_Money)

 call cphlde

 ;If HL is greater than DE (meaning the carry flag
 ;is unaffected), the player has enough money to purchase
 ;the building.

 jr nc, _

 ;The player does not have enough money, and he
 ;needs to know that.

 SET Draw_Standard_Alert_Box, (IY + flag + 2)
 
 ld hl, Not_Enough_Crystals

 call Standard_Alert_Standard_Data


 call Rotate_Cursor_Mode
 SET Menu_Mode, (IY + flag)



 ret

_

 
 RES Menu_Mode, (IY + flag)

 ld ix, TileData			;IX contains the location of the sprites for the tiles.

 add ix, bc
 add ix, bc
 ld hl, Cursor_Building_Space		;Where the sprite for the building image of the cursor
					;will be held
 

 cp 1
 jp z, Cursor_32x16_Building
 cp 2
 jp z, Cursor_16x32_Building
 cp 3
 jp z, Cursor_16x16_Building


Cursor_32x32_Building:

	ld a, 32
	ld (hl), a
	inc hl
	ld a, 4
	ld (hl), a
	inc hl
	ld a, 128
	ld (hl), a
	inc hl

	ld b, 16

_
	
	ld a, (IX)
	ld (hl), a
	inc hl
	ld a, (IX + 1)
	ld (hl), a
	inc hl
	ld a, (IX + 32)
	ld (hl), a
	inc hl
	ld a, (IX + 33)
	ld (hl), a
	inc hl
	inc ix
	inc ix
	djnz -_
	ld b, 16
	ld de, 32
	add ix, de
_

	ld a, (IX)
	ld (hl), a
	inc hl
	ld a, (IX + 1)
	ld (hl), a
	inc hl
	ld a, (IX + 32)
	ld (hl), a
	inc hl
	ld a, (IX + 33)
	ld (hl), a
	inc hl
	inc ix
	inc ix
	djnz -_

	jp Building_Cursor_Saved


Cursor_32x16_Building:


	ld a, 16
	ld (hl), a
	inc hl
	ld a, 4
	ld (hl), a
	inc hl
	ld a, 64
	ld (hl), a
	inc hl




	ld b, 32

_
	
	ld a, (IX)
	ld (hl), a
	inc hl
	ld a, (IX + 1)
	ld (hl), a
	inc hl
	ld a, (IX + 32)
	ld (hl), a
	inc hl
	ld a, (IX + 33)
	ld (hl), a
	inc hl
	inc ix
	inc ix
	djnz -_
	jp Building_Cursor_Saved

Cursor_16x32_Building:


	ld a, 32
	ld (hl), a
	inc hl
	ld a, 2
	ld (hl), a
	inc hl
	ld a, 64
	ld (hl), a
	inc hl

	push ix
	pop de
	ex de, hl
	ld bc, 64
	ldir


	jr Building_Cursor_Saved

Cursor_16x16_Building:

	ld a, 16
	ld (hl), a
	inc hl
	ld a, 2
	ld (hl), a
	inc hl
	ld a, 32
	ld (hl), a
	inc hl

	push ix
	pop de

	ex de, hl
	ld bc, 32
	ldir


Building_Cursor_Saved:

 SET Cursor_Bitmap_Done, (IY + Flag)
 ld hl, Cursor_Building_Space		;Load x, y positions and height/width
 ld b, (hl)
 inc hl
 ld a, (hl)
 ld (Sprite_Width), a
 inc hl
 




 inc hl
 push hl

 pop ix


 ld a, (Select_Cursor_Y)

 ld l, a
 ld a, (Select_Cursor_X)

 call Put_Sprite_Overwrite



 ret 

Select_Cursor:

	.db $00,$00
	.db $01,$00
	.db $02,$80
	.db $05,$40
	.db $05,$40
	.db $1D,$70
	.db $21,$08
	.db $5E,$F4
	.db $21,$08
	.db $1D,$70
	.db $05,$40
	.db $05,$40
	.db $02,$80
	.db $01,$00
	.db $00,$00
	.db $00,$00

Select_Cursor_Mask:

	.db %11111111,%11111111
	.db %11111110,%11111111
	.db %11111100,%01111111
	.db %11111000,%00111111
	.db %11111000,%00111111
	.db %11100000,%00001111
	.db %11000000,%00000111
	.db %10000000,%00000011
	.db %11000000,%00000111
	.db %11100000,%00001111
	.db %11111000,%00111111
	.db %11111000,%00111111
	.db %11111100,%01111111
	.db %11111110,%11111111
	.db %11111111,%11111111
	.db %11111111,%11111111




InfoBox:

.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000
.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111

